package com.beiye.drivertransport.SubActivity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.utils.UserManger;
import com.githang.statusbar.StatusBarCompat;


import butterknife.OnClick;

public class H5PageActivity extends TwoBaseAty {


    TextView acWhiteTitleTvTitle;

    ImageView acWhiteTitleIvBack;

    WebView wv;

    RelativeLayout acThemeRl;

    @Override
    public int getLayoutId() {
        return R.layout.activity_h5_page;
    }

    @Override
    public void initData() {
        acWhiteTitleTvTitle = (TextView) findViewById(R.id.ac_whiteTitle_tv_title);
        acWhiteTitleIvBack = (ImageView) findViewById(R.id.ac_whiteTitle_iv_back);
        wv = (WebView) findViewById(R.id.wv);
        acThemeRl = (RelativeLayout) findViewById(R.id.ac_theme_rl);

        Bundle bundle=getIntent().getExtras();
        String url=bundle.getString("url");
        String title=bundle.getString("title");
        boolean showTitle=bundle.getBoolean("showTitle");


        acWhiteTitleTvTitle.setText(title);
        if(!showTitle){
            acThemeRl.setVisibility(View.GONE);
        }


        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        WebSettings settings = wv.getSettings();
        settings.setSavePassword(false);
        settings.setSaveFormData(false);
        wv.removeJavascriptInterface("accessibility");
        wv.removeJavascriptInterface("accessibilityTraversal");
        settings.setAllowFileAccessFromFileURLs(false);
        // 允许使用脚本语言
        settings.setJavaScriptEnabled(true);
        // 支持缩放
        settings.setSupportZoom(true);


//        //post访问需要提交的参数  
//        String postDate="userId="+userId;
//
//        wv.postUrl(AppInterfaceConfig.TmallUrl, EncodingUtils.getBytes(postDate,"BASE64"));


        // 在当前应用加载网页(直接加载网页不经过浏览器）
        wv.setWebViewClient(new WebViewClient() {
            @Override // 开始加载网页时触发
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override // 网页加载完成之后触发
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

        wv.loadUrl(url);


        // 显示进度
        wv.setWebChromeClient(new WebChromeClient() {
            @Override // 加载网页进度改变时触发
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    public void requestData() {

    }


    public void onClick(View view) {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wv.canGoBack()) {
            // 返回上一页面
            wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            wv.getSettings().setSavePassword(false);
            wv.getSettings().setSaveFormData(false);
            wv.removeJavascriptInterface("accessibility");
            wv.removeJavascriptInterface("accessibilityTraversal");
            wv.getSettings().setAllowFileAccessFromFileURLs(false);
            wv.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
